package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.SysDept;
import com.baomidou.ant.mapper.SysDeptMapper;
import com.baomidou.ant.service.ISysDeptService;
import com.baomidou.ant.vo.request.RequestListDeptVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Override
    public Integer queryByDeptName(String deptName) {
        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("dept",deptName);
        SysDept sysDept=getOne(queryWrapper);
        //返回id
        return sysDept.getId();
    }

    @Override
    public boolean queryName(String deptName) {
        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dept",deptName);
        SysDept sysDept=getOne(queryWrapper);
        //返回id
        return ObjectUtils.isNotEmpty(sysDept);
    }

    @Override
    public IPage<SysDept> list(RequestListDeptVo requestListDeptVo) {
        IPage<SysDept> deptIPage=new Page<>(requestListDeptVo.getDisplayStart(),requestListDeptVo.getDisplayLength());
        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(requestListDeptVo.getDept())){
            queryWrapper.like("dept",requestListDeptVo.getDept());
        }
        IPage<SysDept>  page=page(deptIPage,queryWrapper);
        return page.convert(sysDept -> {
           SysDept dept=new SysDept();
           BeanUtils.copyProperties(sysDept,dept);
           if(sysDept.getErpId()==null){
               dept.setErpId("未对应ERP");
           }
           return dept;
        });
    }

    @Override
    public SysDept queryOne(String FprdOrgId) {
        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("erp_id",FprdOrgId);
        if(list(queryWrapper)==null||list(queryWrapper).size()==0){
            return null;
        } else
            return list(queryWrapper).get(0);
    }
}
